package com.zjdiepu.www.rest.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.TongrongStatus;
import com.zjdiepu.www.base.enums.type.CreditPeopleType;
import com.zjdiepu.www.base.enums.type.CreditResult;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.ZhppDateUtil;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessCarVoMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.other.IOtherAuditInfoService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessAppService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessCarService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.ibiz.yw.IYwTongrongService;
import com.zjdiepu.www.ibiz.yw.IYwTransferService;
import com.zjdiepu.www.ibiz.zpp.FengKongShenHeService;
import com.zjdiepu.www.ibiz.zpp.YwShouLiService;
import com.zjdiepu.www.ibiz.zx.IZxQueryCreditAppService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dz.dto.DzSignLoanAmount;
import com.zjdiepu.www.model.other.dto.OtherAuditInfo;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwLoanAbandon;
import com.zjdiepu.www.model.yw.dto.YwTongrong;
import com.zjdiepu.www.model.yw.dto.YwTransfer;
import com.zjdiepu.www.model.zpp.pub.ext.ParamsExt;

@RestController
@RequestMapping(value= "YwBusinessAppController", produces="application/json;charset=UTF-8")
@Api(tags = {"本月新增业务数量app接口"}, produces="application/json;charset=UTF-8")
public class YwBusinessAppController {

	@Resource
	private IYwBusinessAppService ywBusinessAppService;
	@Resource
    private TokenUserHelper tokenUserHelper;
	@Resource
    private IYwTongrongService ywTongrongService;
	@Resource
    private FengKongShenHeService fengKongShenHeService;
	@Resource
    private IYwBusinessService ywBusinessService;
	@Resource
	private IZxQueryCreditAppService zxQueryCreditAppService;
	@Resource
	private YwShouLiService YwShouLiService;
	@Resource
	private IOtherAuditInfoService iOtherAuditInfoService;
	@Resource
	private IYwTransferService iYwTransferService;
	@Resource
	private IYwBusinessCarService iYwBusinessCarService;
	@Resource
    private ICachedDictionaryService cachedDictionaryService;
	@Resource
	private YwBusinessCarVoMapper ywBusinessCarVoMapper;

	
	@ApiOperation(value = "统计当月新增的业务数量",response = Integer.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "countBusinessThisMonth", method = RequestMethod.POST)
	public ApiResponse countBusinessThisMonth(){
		 SysUser currentUser = tokenUserHelper.getUser();
		DzSignLoanAmount dzSignLoanAmount= new DzSignLoanAmount();
		dzSignLoanAmount.setSalersmanId(currentUser.getSalersmanId());
		int count=ywBusinessAppService.countBusinessThisMonth(dzSignLoanAmount);
		Map<String,Object> response=CollectionInitUtil.emptyMap();
		response.put("count", count);
		return ApiResponse.success(response);
	}
	
	@ApiOperation(value = "业务-申请通融",response = Integer.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "applyTongRong", method = RequestMethod.POST)
	public ApiResponse applyTongRong(@Validated @RequestBody YwTongrong ywTongrong){
		SysUser currentUser = tokenUserHelper.getUser();
    	ywTongrong.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	ywTongrong.setModifierName(currentUser.getName());
    	ywTongrong.setStatusTongrong(TongrongStatus.TONGRONGING.getCode());
        ywTongrongService.saveUpdateAndDelete(ywTongrong, OperateType.add);
        fengKongShenHeService.diepuFengKongShenTrsh(currentUser,ywTongrong.getBusiId(),ModeIDType.TRQYJLSH.getCode());
        //修改业务表状态
        YwBusiness ywBusiness = new YwBusiness();
    	ywBusiness.setId(ywTongrong.getBusiId());
    	ywBusiness.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	ywBusiness.setModifierName(currentUser.getName());
    	ywBusiness.setTongrongStatus(TongrongStatus.TONGRONGING.getCode());
        ywBusinessService.saveUpdateAndDelete(ywBusiness, OperateType.edit);
//    	ywBusinessService.insertFkAuditList(ywTongrong.getBusiId(),ModeIDType.TRSH.getCode(),currentUser);
		return ApiResponse.success(true);
	}
	@ApiOperation(value = "风控列表",response = Integer.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "fengkongList", method = RequestMethod.POST)
	public ApiResponse fengkongList(@Validated @RequestBody com.zjdiepu.www.model.zpp.fksh.ext.ParamsExt paramsExt){
		tokenUserHelper.getUser();
		OtherAuditInfo otherAuditInfo = new OtherAuditInfo();
		otherAuditInfo.setBusiId(paramsExt.getBusiId());
		otherAuditInfo.setAuditStatus("3");
		OtherAuditInfo otherAuditInfo1=iOtherAuditInfoService.selectRefined(otherAuditInfo);
		try {
			otherAuditInfo1.setCreateDate(ZhppDateUtil.stampConvertDate(otherAuditInfo1.getCreated(), "yyyy-MM-dd"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Map<String,Object> response=CollectionInitUtil.emptyMap();
		response.put("result", otherAuditInfo1);
		return ApiResponse.success(response);
	}
	
	@ApiOperation(value = "统计风控未通过、征信未通过 、成功客户",response = Integer.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "countZxAndFengkongAndSuccess", method = RequestMethod.POST)
	public ApiResponse countZxAndFengkongAndSuccess(){
		//成功客户
		SysUser currentUser = tokenUserHelper.getUser();
		DzSignLoanAmount dzSignLoanAmount= new DzSignLoanAmount();
		dzSignLoanAmount.setSalersmanId(currentUser.getSalersmanId());
		dzSignLoanAmount.setBusiStatus(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
		int count=ywBusinessAppService.countFengkongLastMonth(dzSignLoanAmount);
		Map<String,Object> response=CollectionInitUtil.emptyMap();
		response.put("successCount", count);
		
		//风控未通过
		dzSignLoanAmount= new DzSignLoanAmount();
		dzSignLoanAmount.setSalersmanId(currentUser.getSalersmanId());
		dzSignLoanAmount.setBusiStatusLess(BusiStatus.CHU_SHEN_DAN_CHU_SHEN_SHI_BAI.getCode());
		dzSignLoanAmount.setBusiStatusMore(BusiStatus.FENG_KONG_ZONG_JIAN_SHEN_HE_SHI_BAI.getCode());
		int fkCount=ywBusinessAppService.countFengkongLastMonth(dzSignLoanAmount);
		response.put("fkCount", fkCount);
		
		dzSignLoanAmount= new DzSignLoanAmount();
		dzSignLoanAmount.setSalersmanId(currentUser.getSalersmanId());
		dzSignLoanAmount.setCreditPeopleType(CreditPeopleType.ZHU_JIE_REN.getCode());
		dzSignLoanAmount.setCreditResult(CreditResult.DEBAR.getCode());
		int zxCount=zxQueryCreditAppService.countZxQueryResultCredit(dzSignLoanAmount);
		response.put("zxCount", zxCount);
		return ApiResponse.success(response);
	}
	/**
	 * 过户登记
	 * @param paramsExt
	 * @return
	 */
	@ApiOperation(value = "过户登记",response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "saveKeHuDengJiService", method = RequestMethod.POST)
	public ApiResponse saveKeHuDengJiService(@Validated @RequestBody ParamsExt paramsExt)
	{
		SysUser currentUser = tokenUserHelper.getUser();
		YwBusinessCar ywBusinessCar=paramsExt.getYwBusinessCar();
		YwShouLiService.saveKeHuDengJiService(paramsExt, currentUser,ywBusinessCar);
		return ApiResponse.success(true);
	}
	/**
	   * 根据业务id获取过户登记信息
	   * @param paramsExt
	   * @return
	   */
	  @ApiOperation(value = "根据业务id获取过户登记信息",response = Boolean.class, httpMethod="POST")
	    @ApiImplicitParams({
	      @ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
	      @ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
	    })
	    @RequestMapping(value = "getKeHuDengJiInfoByBusiId", method = RequestMethod.POST)
	  public ApiResponse getKeHuDengJiInfoByBusiId(@RequestBody YwTransfer ywTransfer)
	  {
		  	/**update 查询条件用原始对象 2017-11-22*/
		  	Map<String,Object> map = CollectionInitUtil.emptyMap();
		    YwBusinessCar ywBusinessCar=new YwBusinessCar();
		    ywBusinessCar.setBusiId(ywTransfer.getBusiId());
		    ywBusinessCar.setDpStatus(DpStatus.NORMAL.getCode());
		    ywBusinessCar = iYwBusinessCarService.selectRefined(ywBusinessCar);
		    if(ywBusinessCar!=null){
		    	ywBusinessCarVoMapper.mapper(ywBusinessCar);
		    	map.put("ywBusinessCar", ywBusinessCar);
		    }
		    //过户资料
		    ywTransfer.setDpStatus(DpStatus.NORMAL.getCode());
		    ywTransfer = iYwTransferService.selectRefined(ywTransfer);
		    if(ywTransfer!=null){
		    	map.put("ywTransfer", ywTransfer);
		    }
		    return ApiResponse.success(map);
	  }
	@ApiOperation(value = "弃贷",response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "ywShouLiQiDaiBiao", method = RequestMethod.POST)
	public ApiResponse ywShouLiQiDaiBiao(@Validated @RequestBody YwLoanAbandon ywLoanAbandon)
	{
		SysUser currentUser = tokenUserHelper.getUser();
		YwBusiness ywBusiness= new YwBusiness();
		ywBusiness.setId(ywLoanAbandon.getBusiId());
		ywBusiness.setBusiStatus(BusiStatus.QI_DAI.getCode());
		YwShouLiService.saveYwShouLiQingDaiBiao(ywLoanAbandon, ywBusiness, currentUser);
		return ApiResponse.success(true);
	}
}
